import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/ins_task_detail_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class SupplementaryInspectionController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 获取任务列表数据
  Future<List<InsTaskDetailEntity>> getListData(int pageNum, int pageSize) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['taskName'] = search;

    // 获取产品列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<InsTaskDetailEntity>>(
          Interface.hazardInvestigationSupplementaryInspection,
          queryParameters: queryParameters,
        )
        .execute();
    List<InsTaskDetailEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 进入详情页面
  void toDetailsPage(String id) {
    Get.toNamed(AppRoutes.hazardSupplementaryDetailsPage, arguments: {
      'title': '补检任务',
      'taskId': id,
    })?.then((value) {
      if(value == 'success') {
        refreshPage();
      }
    });
  }
}
